
#include "ping.h"

void proc_v4(char * buf, ssize_t len, struct msghdr * msg, struct timeval * tvrecv) {

    int hlen1 = 0, icmplen = 0;
    double rtt;
    struct ip * ip;
    struct icmp * icmp;

    struct timeval * tvsend;

    ip = (struct ip *)buf;
    hlen1 = sizeof(struct ip);
    if (ip->ip_p != IPPROTO_ICMP) {

        printf("protocol error and ip->ip_p : %d\n", ip->ip_p);
        return;
    }
    icmp = (struct icmp * )(buf + hlen1);
    if ((icmplen = len - hlen1) < 8) {

        printf("length is not enough\n");
        return;
    }
    if (icmp->icmp_type == ICMP_ECHOREPLY) {

        if (icmp->icmp_id != Instance()->pid) {

            printf("is not belong to this pid : %d\n", icmp->icmp_id);
            return;
        }
        if (icmplen < 16) {

            printf("icmplen is %d < 16\n", icmplen);
            return;
        }
        tvsend = (struct timeval * )icmp->icmp_data;
        tv_sub(tvrecv, tvsend);

        rtt = tvrecv->tv_sec * 1000 + tvrecv->tv_usec / 1000;

        char ip_buf[32];
        inet_ntop(AF_INET, (struct sockaddr_in *)Instance()->pr->sarecv, ip_buf, 32);
        printf("%d bytes from %s: seq=%u, ttl=%d, rtt=%.3f ms\n", icmplen, ip_buf, icmp->icmp_seq, ip->ip_ttl, rtt);
    }
}

void proc_v6(char * buf, ssize_t len, struct msghdr * msg, struct timeval * tvrecv) {


}

void send_v6() {


}

void init_v6() {


}